<template>
  <div>
    <h1>图书详情库</h1>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="id" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px"><img :src="scope.row.cover" /></span>
        </template>
      </el-table-column>
      <el-table-column label="书名" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作者" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出版日期" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出版社" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.press }}</span>
        </template>
      </el-table-column>
      <el-table-column label="简介" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.abs }}</span>
        </template> </el-table-column
      ><el-table-column label="分类" width="180" height="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.cid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- <edit-dialog ref="editdialog"></edit-dialog> -->
    <edit-form ref="editform" @onSubmit="loadBooks()"></edit-form>
  </div>
</template>

<script>
import EditForm from "./EditForm.vue";
export default {
  data() {
    return {
      tableData: [
        {
          id: "",
          cover: "",
          title: "",
          author: "",
          date: "",
          press: "",
          abs: "",
          cid: "",
        },
      ],
    };
  },
  components: { EditForm },
  methods: {
    loadBooks() {
      console.log(6666);
      var _this = this;
      this.$axios.get("/books").then((resp) => {
        if (resp && resp.status === 200) {
          _this.tableData = resp.data.reverse();
          console.log(this.tableData);
        }
      });
    },
    handleEdit(index, row) {
      this.$refs.editform.dialogFormVisible = true;

      // this.$refs.edit.dialogFormVisible = true

      this.$refs.editform.form = {
        id: this.tableData[index].id,
        cover: this.tableData[index].cover,
        title: this.tableData[index].title,
        author: this.tableData[index].author,
        date: this.tableData[index].date,
        press: this.tableData[index].press,
        abs: this.tableData[index].abs,
        category: {
          id: this.tableData[index].category.id.toString(),
          name: this.tableData[index].category.name,
        },
      };
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该书籍, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.post("/delete", row).then((resp) => {
            this.loadBooks();
            this.deleteMessage();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteMessage() {
      this.$message({
        message: "删除成功",
        type: "success",
      });
    },
  },
  mounted: function () {
    this.loadBooks();
  },
};
</script>
<style>
img {
  width: 115px;
  height: 172px;
  /*margin: 0 auto;*/
}
/* .el-icon-circle-plus-outline:before {
    position: absolute;
    content: "\E723";
    margin-top: -6300px;
} */
</style>